/*
 * Copyright(C) 2012 Luvina Software Company
 *
 * StringUtill.java, Jun 7, 2012 HaiPD
 */
package net.luvina.manageuser.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * String Utill
 *
 * @author HaiPD
 *
 */
public class StringUtill {
	/**
	 * check Min Max Leng
	 *
	 * @param value
	 *            value
	 * @param min
	 *            min
	 * @param max
	 *            max
	 * @return true if ok, false if not ok
	 */
	public static boolean checkMinMaxLeng(String value, int min, int max) {
		if (value.length() < min || value.length() > max) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * check halfsize
	 *
	 * @param value
	 *            value
	 * @return true if ok, false if not ok
	 */
	public static boolean isHalfsize(String value) {
		String halfsize_pattern = "[0-9]+";
		Pattern pattern = Pattern.compile(halfsize_pattern);
		Matcher matcher = pattern.matcher(value);
		if (matcher.matches()) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * Check katakana
	 *
	 * @param text
	 *            string
	 * @return true if check ok
	 */
	public static boolean isKatakana(String text) {
		char[] c = text.toCharArray();

		for (int i = 0; i < c.length; i++) {
			if ((Character.UnicodeBlock.of(c[i]) != Character.UnicodeBlock.KATAKANA)
					&& (!isDigit(c[i])) && (!Character.isWhitespace(c[i]))) {

				return false;
			}
		}
		return true;
	}

	/**
	 * isDigit check type number
	 *
	 * @param c
	 *            char
	 * @return true is check ok
	 */
	public static boolean isDigit(char c) {
		int x = (int) c;

		if ((x >= 48) && (x <= 57)) {
			return true;
		}

		return false;
	}

	/**
	 * Check for japan char
	 * @param c char
	 * @return true if ok
	 */
	public static boolean isJapanese(char c) {
		if (c >= '\u30a0' && c <= '\u30ff')
			return true;
		if (Character.UnicodeBlock.of(c) == Character.UnicodeBlock.KATAKANA)
			return true;
		if (c >= '\u3040' && c <= '\u309f')
			return true;
		if (Character.UnicodeBlock.of(c) == Character.UnicodeBlock.HIRAGANA)
			return true;
		if (c >= '\u4e00' && c <= '\u9fff')
			return true;
		if (c >= '\u3000' && c <= '\u303f')
			return true;
		if (c >= '\u2f00' && c <= '\u2fdf')
			return true;
		if (c >= '\u3190' && c <= '\u319f')
			return true;
		if (c >= '\u3400' && c <= '\u4db5')
			return true;
		if (c >= '\ufe30' && c <= '\ufe4f')
			return true;
		if (c >= '\u3300' && c <= '\u33ff')
			return true;
		if (c >= '\u2e80' && c <= '\u2eff')
			return true;
		return false;
	}

	/**
	 * Check 2 byte char
	 * @param text string
	 * @return
	 */
	 public static boolean isTwoByteCharater(String text) {
	        char[] c = text.toCharArray();

	        for (int i = 0; i < c.length; i++) {
	            if (!isJapanese(c[i]) &&
	            /*(!isDigit(c[i])) &&*/
	            (!Character.isWhitespace(c[i]))) {

	            return false;
	            }
	        }
	        return true;
	    }
}
